package gold.digger;

import gold.utils.InputUtil;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC452 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int findMinArrowShots(int[][] points) {
            if (null == points || points.length <= 0) return 0;

            Arrays.sort(points, new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    return o1[0] > o2[0] ? 1 : -1;
                }
            });

            int count = 1, curStart = points[0][0], curEnd = points[0][1];
            for (int[] point : points) {
                if (point[0] > curEnd) {
                    ++count;
                    curStart = point[0];
                    curEnd = point[1];
                    continue;
                } else {
                    //有重合
                    curEnd = Math.min(curEnd, point[1]);
                    curStart = Math.max(curStart, point[0]);
                }
            }

            return count;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[][] arr = InputUtil.toDoubleIntegerArray("[[9,12],[1,10],[4,11],[8,12],[3,9],[6,9],[6,7]]");
        System.out.println(solution.findMinArrowShots(arr));
    }

    public static void main(String[] args) throws Exception {
        LC452 an = new LC452();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
